package com.dc.basic.controller;

import com.alibaba.fastjson.JSONObject;
import com.dc.basic.dao.user.UserInfoMapper;
import com.dc.basic.domain.user.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private com.dc.basic.service.user.UserInfoService UserInfoService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Value("${user.env}")
    private String userEvn;

    @GetMapping("/user/list")
    public String userList() {
        List<UserInfo> list = UserInfoService.list();
        log.info("userList data={}",JSONObject.toJSONString(list));
        Integer i = userInfoMapper.selectCount();
        JSONObject object = new JSONObject();
        object.put("count",i);
        object.put("list",list);
        return object.toString();
    }

    @GetMapping("/user/evn")
    public String userEvn() {

        return userEvn;
    }

    @PostMapping("/user/save")
    public Boolean userSave(@RequestBody UserInfo userInfo){
        log.info("userSave save={}", JSONObject.toJSONString(userInfo));
        boolean save = UserInfoService.save(userInfo);
        return save;
    }
}
